package com.woniu.airent.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.woniu.airent.realm.MyShiroRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //配置拦截器
        Map<String,String> filter = new LinkedHashMap<>();
        //退出登录
       filter.put("/logout","logout");
       filter.put("/**","anon");
    /*     //不拦截登录页面和样式
        filter.put("/toLogin","anon");
        filter.put("/user/login","anon");
        filter.put("toRegister","anon");
        filter.put("/user/reg","anon");

        //其他页面需要认证才能访问
        filter.put("/toDashBoard","authc");
        filter.put("/user/**","authc");
        filter.put("/staff/list","authc,roles[staff]");
        filter.put("/staff/**","authc,roles[admin]");
        filter.put("/department/**","authc,roles[staff]");
        filter.put("templates/**","authc");
        //       filter.put("/static/**","authc");*/

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filter);

        //不登录就跳转到登录页面
        shiroFilterFactoryBean.setLoginUrl("/login_register.html");

        //登录成功后跳转页面
        shiroFilterFactoryBean.setSuccessUrl("/index.html");

    //    shiroFilterFactoryBean.setUnauthorizedUrl("/toNoAuth");

        return shiroFilterFactoryBean;
    }

    @Bean(name = "securityManager")
    public SecurityManager securityManager(@Qualifier("myShiroRealm") MyShiroRealm myShiroRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myShiroRealm);
        return defaultWebSecurityManager;
    }

    @Bean("hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //指定加密方式为MD5
        credentialsMatcher.setHashAlgorithmName("MD5");
        //加密次数
        credentialsMatcher.setHashIterations(1024);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }

    @Bean(name = "myShiroRealm")
    public MyShiroRealm myShiroRealm(@Qualifier("hashedCredentialsMatcher") HashedCredentialsMatcher hashedCredentialsMatcher){
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myShiroRealm;
    }

    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }

}
